package com.isunland.managesystem.adapter;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import com.android.volley.toolbox.ImageLoader;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseButterKnifeAdapter;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.entity.FunctionObject;
import com.isunland.managesystem.entity.GuideMenu;
import com.isunland.managesystem.utils.ConfigUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import java.util.ArrayList;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/5 15:01
 */

public class GuideCommonAdapter extends BaseButterKnifeAdapter<GuideMenu> {

  private ImageLoader mImageLoader;

  @SuppressWarnings("unused")
  public GuideCommonAdapter(BaseVolleyActivity context, ArrayList<GuideMenu> objects) {
    super(context, objects);
    this.mImageLoader = RequestManager.getImageLoader();
  }

  @Override protected int setLayoutId() {
    return R.layout.adapter_common_menu;
  }

  @NonNull @Override protected BaseViewHolder initHolder(View convertView) {
    return new ViewHolder(convertView);
  }

  @Override protected void initView(final GuideMenu item, BaseViewHolder baseHolder, int position) {
    ViewHolder holder = (ViewHolder) baseHolder;
    //图标显示
    //changeIcon(item.getAlias(), ivIcon);
    String icon = item.getDefaultUrl();
    if (!TextUtils.isEmpty(icon)) {
      if (MyStringUtil.contains(icon, "?")) {
        icon = MyStringUtil.substring(icon, 0, icon.indexOf("?"));
      }
      String urlImage = ApiConst.getDownLoadUrlByGet(icon);
      ImageLoader.ImageListener listener = ImageLoader.getImageListener(holder.ivIcon, // 控件名
          R.drawable.icon_liuchengrenwu, R.drawable.icon_liuchengrenwu); // 默认图标
      mImageLoader.get(urlImage, listener);
    } else {
      holder.ivIcon.setImageResource(R.drawable.icon_liuchengrenwu);
    }

    holder.tvTitle.setText(item.getResName());

    //显示提示数量
    FunctionObject config = ConfigUtil.get(context, MyStringUtil.valueOfInt(item.getAlias(), 0));
    holder.tvHintNumber.setVisibility(
        (config == null || config.getHintNumber() == 0) ? View.GONE : View.VISIBLE);
    holder.tvHintNumber.setText((config == null) ? "" : String.valueOf(config.getHintNumber()));
  }

  protected class ViewHolder extends BaseViewHolder {

    @BindView(R.id.iv_icon) ImageView ivIcon;
    @BindView(R.id.tv_hintNumber) TextView tvHintNumber;
    @BindView(R.id.tv_title) TextView tvTitle;
    //@BindView(R.id.rl_holder) RelativeLayout rlHolder;

    ViewHolder(View view) {
      super(view);
      //点击事件

    }
  }
}
